home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / dmake38c.zip / ENVIRON.C < prev    next >
C/C++ Source or Header  |  1992-01-23  |  7KB  |  237 lines

  1. /* RCS      -- $Header: /u2/dvadura/src/generic/dmake/src/mac/environ.c,v 1.1 1992/01/24 03:29:46 dvadura Exp $
  2. -- SYNOPSIS -- Set up and free for environ
  3. --
  4. -- DESCRIPTION
  5. --  This file contains routines that will fill in and dispose of the
  6. --  list of environmental variables in the environ global variable.
  7. --
  8. -- AUTHOR
  9. --      Dennis Vadura, dvadura@watdragon.uwaterloo.ca
  10. --      CS DEPT, University of Waterloo, Waterloo, Ont., Canada
  11. --
  12. --
  13. -- COPYRIGHT
  14. --      Copyright (c) 1990 by Dennis Vadura.  All rights reserved.
  15. -- 
  16. --      This program is free software; you can redistribute it and/or
  17. --      modify it under the terms of the GNU General Public License
  18. --      (version 1), as published by the Free Software Foundation, and
  19. --      found in the file 'LICENSE' included with this distribution.
  20. -- 
  21. --      This program is distributed in the hope that it will be useful,
  22. --      but WITHOUT ANY WARRANTY; without even the implied warrant of
  23. --      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24. --      GNU General Public License for more details.
  25. -- 
  26. --      You should have received a copy of the GNU General Public License
  27. --      along with this program;  if not, write to the Free Software
  28. --      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29. --
  30. -- LOG
  31. --     $Log: environ.c,v $
  32.  * Revision 1.1  1992/01/24  03:29:46  dvadura
  33.  * dmake Version 3.8, Initial revision
  34.  *
  35. */
  36.  
  37. #include "extern.h"
  38.  
  39. /* The char used to replace the equal signs in environmental variable names. */
  40. const char kEqualReplace = '_';
  41.  
  42. /* Maximum size of a "name=value" environmental string, including the ending '\0'.
  43.    Larger environmental variables will be clipped before dmake sees them.
  44.    (Caution: When I tested the program, the Mac or dmake trashed memory
  45.     when environmental variables of >4K were read in.  I looked around a bit
  46.     and couldn't find out the exact cause, so I simply made this variable.
  47.     The memory trashing may be related to the value for MAXLINELENGTH.) */
  48. const int kMaxEnvLen = 1024;
  49.  
  50.  
  51. /* The list of environmental variables in the form "name=value".
  52.    (Once make_env() has been called.) */
  53. char **environ = NULL;
  54.  
  55. /* Characters replaced during make_env() */
  56. struct ReplaceChar {
  57.     char *fpPos;
  58.     char fC;
  59.     struct ReplaceChar *fpNext;
  60. }; /* struct ReplaceChar */
  61. struct ReplaceChar *gpReplaceList = NULL;
  62.  
  63.  
  64. void AddReplace (char *pReplacePos);
  65.  
  66.  
  67.  
  68. /*
  69.  * Set up the environmental variables in a format used by
  70.  * the environ global variable.
  71.  *
  72.  * environ has already been set to main's envp argument when
  73.  * this suboroutine is called.  We assume that envp is a copy
  74.  * MPW makes for this process' use alone, so we can modify it
  75.  * below.
  76.  */
  77. PUBLIC void make_env () {
  78.     char **ppCurEnv;
  79.     char *pCurPos;
  80. #if 0
  81.     char **ppMacEnv;
  82.     char *pMacPos;
  83.  
  84.     if (!gMECalled) {
  85.         gMECalled = TRUE;
  86.  
  87. environ = MALLOC (1, char *);
  88. *environ = NULL;
  89. #endif
  90. #if 0
  91. {
  92.     int numenv;
  93.     int len;
  94.     int firstnil;
  95.  
  96.     numenv = 1;
  97.     ppMacEnv = environ;
  98.     while (*(ppMacEnv++) != NULL) {
  99.         ++numenv;
  100.     } /* while */
  101.  
  102.     ppMacEnv = environ;
  103.     if ((environ = MALLOC (numenv, char *)) == NULL) {
  104.         No_ram ();
  105.     } /* if */
  106.  
  107. numenv = 80;
  108.     for (ppCurEnv = environ; (numenv-- > 0) && (*ppMacEnv != NULL); ++ppCurEnv, ++ppMacEnv) {
  109.         pMacPos = *ppMacEnv;
  110.         len = strlen (pMacPos) + 1;
  111.         len += strlen (pMacPos + len) + 1;
  112. #define MAXLEN 4098
  113. if (len > MAXLEN) len = MAXLEN;
  114.         if ((*ppCurEnv = MALLOC (len, char)) == NULL) {
  115.             No_ram ();
  116.         } /* if */
  117.  
  118.         firstnil = TRUE;
  119.         for (pCurPos = *ppCurEnv; ((pCurPos - *ppCurEnv) < MAXLEN - 1); ++pCurPos, ++pMacPos) {
  120.             if (*pMacPos == '=') {
  121.                 *pCurPos = gEqualReplace;
  122.  
  123.             } else if (*pMacPos == '\0') {
  124.                 if (firstnil) {
  125.                     *pCurPos = '=';
  126.                     firstnil = FALSE;
  127.                 } else {
  128.                     *pCurPos = *pMacPos;
  129.                     break;
  130.                 } /* if ... else */
  131.  
  132.             } else {
  133.                 *pCurPos = *pMacPos;
  134.             } /* if ... elses */
  135.         } /* for */
  136. firstnil = FALSE;
  137.     } /* for */
  138.     *ppCurEnv = NULL;
  139. }
  140. #endif
  141. {
  142.         int firstnil;
  143.  
  144.         /* Get rid of any equal signs in any environmental name, and put
  145.            equal signs between the names and their values */
  146.         for (ppCurEnv = environ; *ppCurEnv != NULL; ++ppCurEnv) {
  147.  
  148.             firstnil = TRUE;
  149.             for (pCurPos = *ppCurEnv;
  150.                  ((pCurPos - *ppCurEnv < kMaxEnvLen - 1) &&
  151.                   ((*pCurPos != '\0') || !firstnil));
  152.                  ++pCurPos) {
  153.                 if (*pCurPos == '=') {
  154.                     AddReplace (pCurPos);
  155.                     *pCurPos = kEqualReplace;
  156.  
  157.                 } else if (*pCurPos == '\0') {
  158.                     AddReplace (pCurPos);
  159.                     *pCurPos = '=';
  160.                     firstnil = FALSE;
  161.                 } /* if ... else if */
  162.             } /* for */
  163.  
  164.             /* If the environtmental variable was too large ... */
  165.             if (*pCurPos != '\0') {
  166.                 AddReplace (pCurPos);
  167.                 *pCurPos = '\0';
  168.                 if (firstnil) {
  169.                     AddReplace (--pCurPos);
  170.                     *pCurPos = '=';
  171.                 } /* if */
  172.             } /* if */
  173.         } /* for */
  174. }
  175. #if 0
  176.     } /* if */
  177. #endif
  178. } /* PUBLIC void make_env () */
  179.  
  180.  
  181. /*
  182.  * The character at pReplacePos is about to be replaced.  Remember the
  183.  * old value so we can restore it when we're done.
  184.  */
  185. void AddReplace (char *pReplacePos) {
  186.     struct ReplaceChar *pReplaceChar;
  187.  
  188.     if ((pReplaceChar = MALLOC (1, struct ReplaceChar)) == NULL) {
  189.         No_ram ();
  190.     } /* if */
  191.     pReplaceChar->fpPos = pReplacePos;
  192.     pReplaceChar->fC = *pReplacePos;
  193.     pReplaceChar->fpNext = gpReplaceList;
  194.     gpReplaceList = pReplaceChar;
  195. } /* void AddReplace () */
  196.  
  197.  
  198. /*
  199.  * Restore the old environmental variables to the way they looked before
  200.  * the make_env() call, on the unlikely chance that something else will look
  201.  * at our copy of the environmental variables during the program execution.
  202.  * 
  203.  */
  204. PUBLIC void free_env () {
  205.     struct ReplaceChar *pReplaceChar;
  206.  
  207.     while (gpReplaceList != NULL) {
  208.         pReplaceChar = gpReplaceList;
  209.         gpReplaceList = pReplaceChar->fpNext;
  210.  
  211.         *(pReplaceChar->fpPos) = pReplaceChar->fC;
  212.  
  213.         FREE (pReplaceChar);
  214.     } /* while */
  215.  
  216. #if 0
  217.     char **ppCurEnv;
  218.     char *pCurPos;
  219.  
  220.     if (!gFECalled) {
  221.         gFECalled = TRUE;
  222.  
  223. //FREE (environ);
  224. environ = NULL;
  225. #endif
  226. #if 0
  227.         /* Restore the environment list to what it was before we
  228.            read it in. */
  229.         for (ppCurEnv = environ; *ppCurEnv != NULL; ++ppCurEnv) {
  230.             for (pCurPos = *ppCurEnv; *pCurPos != '='; ++pCurPos)
  231.                 ;
  232.             *pCurPos = '\0';
  233.         } /* for */
  234.     } /* if */
  235. #endif
  236. } /* PUBLIC void free_env () */
  237.